/**
 * Project Name:WxpayTradePrecreate.java
 * File Name:WxpayTradePrecreate.java
 * Date:2018/8/2 下午6:35
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.base.pay.utils;

import com.alibaba.fastjson.JSON;
import com.meatball.base.pay.cons.PayConstant;
import com.meatball.base.pay.order.model.PayOrder;
import com.meatball.utils.pay.weixin.WXPay;
import com.meatball.utils.pay.weixin.WXPayConfigImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: WxpayTradePrecreate.java
 * @Description: todo()
 * @Author 張翔宇
 * @Date 2018/8/2 下午6:35
 * @Version V1.0
 */
@Slf4j
@Component
public class WxpayTradePrecreate {
    private WXPay wxpay;
    private WXPayConfigImpl config;

    public WxpayTradePrecreate() throws Exception {
        config = WXPayConfigImpl.getInstance();
        wxpay = new WXPay(config);
    }

    public Map<String, String> doUnifiedOrder(PayOrder payOrder){
        Map<String, String> result = new HashMap<>(10);
        HashMap<String, String> data = new HashMap<>(10);
        //商品描述
        data.put("body", "南充中心医院-一卡通");
        //商户订单号
        data.put("out_trade_no", payOrder.getOutTradeNo());
        //标价币种 人民币
        data.put("fee_type", "CNY");
        //金额
        data.put("total_fee", payOrder.getTotalAmount().toString());
        //终端ip 微信支付API的机器IP
        data.put("spbill_create_ip", "123.12.12.123");
        //回调地址
        data.put("notify_url", "http://www.jycompany.cn/api/ykt/order/wxback");
        //交易类型 扫码支付
        data.put("trade_type", PayConstant.PAY_CHANNEL_WX_MWEB);
        //设备号
        data.put("device_info", "WEB");
        //商品编号
        data.put("product_id",  payOrder.getId().toString());
        //指定支付方式 不支持信用卡
        data.put("limit_pay", "no_credit");
        try {
            log.info("微信预下单参数：{}", JSON.toJSONString(data));
            result = wxpay.unifiedOrder(data);
            log.info("微信预下单返回结果：{}", JSON.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
